<?php
declare (strict_types = 1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use Afaya\EdgeTTS\Service\EdgeTTS;

class AudioCtx extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('ac')
            ->setDescription('the audio command');
    }

    protected function execute(Input $input, Output $output)
    {
        $text = '你好';
        $output_file = $input->getOption('output') ?? 'output_' . time();
        $voice='zh';
        if($voice == 'en'){
            $ssml = '<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xml:lang="en-US">
                <voice name="en-US-AriaNeural">
                    <prosody rate="0%" pitch="0Hz" volume="0%">
                        ' . $text . '
                    </prosody>
                </voice>
            </speak>';
            $voice = 'en-US-AriaNeural';
        }else{
            $ssml = '<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xml:lang="zh-CN">
                <voice name="zh-CN-XiaoxiaoNeural">
                    <prosody rate="0%" pitch="0Hz" volume="0%">
                        '.$text.'
                    </prosody>
                </voice>
            </speak>';
            $voice = 'zh-CN-XiaoxiaoNeural';
        }
        $tts = new EdgeTTS();
        $tts->synthesize($ssml, $voice, [
            'rate' => '0%',
            'volume' => '0%',
            'pitch' => '0Hz'
        ]);
        $tts->toFile("{$output_file}");
        $output->writeln("Audio file generated: {$output_file}.mp3");
    }
}
